<html>
 <head>
  <title>Test Page for Signals</title>
  <script language="javascript" src="../../jsunit/app/jsUnitCore.js"></script>
  <script language="javascript" src="../compiled/ca-all.js"></script>
 </head>
 <body>
  <script language="javascript">
    function testCreation() {
    	var success = true;
    	try{
			var signal = CA.Signal();
		}catch(e){
			success = false;
		}
		assertTrue("Should succeed with creation",success);
	    success = true;
    	try{
			var signal = CA.Signal(function(){});
		}catch(e){
			success = false;
		}
		assertTrue("Should succeed with creation",success);
    }
	function testWithNoConnections(){
		var signal = CA.Signal();
    	var success = true;
    	try{
			signal();
		}catch(e){
			success = false;
		}
		assertTrue("Should succeed with creation",success);
	}
	function testWithConnections(){
		var signal = CA.Signal();
		var i = 0;
		var signal = CA.Signal();
		signal.Connect( function(){
			i++;
		});
		signal();
		assertEquals("Should equal 1",1,i);
		signal.Connect( function(){
			i=i+2;
		});
		signal();
		assertEquals("Should equal 1",4,i);
	}
	function testSignalScope(){
		var test = { x: 0 ,
		             sig : CA.Signal() };
		test.sig.Connect( function(){
			var last = arguments[arguments.length-1];
			var total = 0;
			for(var i=0;i<arguments.length-1;i++){
			    total = total + arguments[i];
			}
			last.x = total;
		});
		test.sig(4,5);
		assertEquals("Should equal 1",test.x,9);
	}
	function testConnectionScope(){
		var signal = CA.Signal();
		var test = { x: 0 };
		signal.Connect( function(){
			var last = arguments[arguments.length-1];
			var total = 0;
			for(var i=0;i<arguments.length-1;i++){
			    total = total + arguments[i];
			}
			this.x = total;
		},test);
		signal(4,5);
		assertEquals("Should equal 1",test.x,9);
	}
  </script>
 </body>
</html>